<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <script src="../../../vue.js"></script>
  </head>

  <body>
    <div id="app">
      <h1 :style="{opacity:opacity}">Vue真的很不错</h1>
      <button @click="$destroy()">销毁当前实例</button>
    </div>
    <script>
      Vue.config.productionTip = false;

      const vm = new Vue({
        el: "#app",
        data() {
          return {
            opacity: 1,
            timer: null,
          };
        },

        //mounted生命周期函数,当vue挂载完成后执行,生命周期其实也是vue的配置项
        mounted() {
          this.timer = setInterval(() => {
            console.log(1);
            this.opacity -= 0.1;
            if (this.opacity <= 0) {
              this.opacity = 1;
            }
          }, 100);
        },

        //实例销毁也有一个生命周期函数,beforeDestroy
        beforeDestroy() {
          clearInterval(this.timer);
        },
      });
    </script>
  </body>
</html>
